package com.example.servicetest

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.media.MediaPlayer
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.TelephonyManager
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    private lateinit var btn_main_play:Button
    private lateinit var btn_main_stop:Button
    private lateinit var btn_main_pause:Button
    private lateinit var btn_main_exit:Button
    private lateinit var intentService:Intent
    private lateinit var btn_main_start1:Button
    private lateinit var btn_main_stop1:Button


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.btn_main_play=findViewById(R.id.btn_main_play)
        this.btn_main_pause=findViewById(R.id.btn_main_pause)
        this.btn_main_stop=findViewById(R.id.btn_main_stop)
        this.btn_main_exit=findViewById(R.id.btn_main_exit)
        intentService = Intent(this,MusicService::class.java).apply {
            setPackage("com.example.servicetest")
        }

        this.btn_main_start1=findViewById(R.id.btn_main_start1)
        this.btn_main_stop1=findViewById(R.id.btn_main_stop1)

    }



    fun startListenCall(view: View) {
        this.intentService.putExtra("action","play")
        startService(intentService)
//        play()
    }

    fun stopListenCall(view: View) {
        this.intentService.putExtra("action","stop")
        startService(intentService)
//        stopMusic()
    }

    fun pauseListenCall(view: View) {
        this.intentService.putExtra("action","pause")
        startService(intentService)
//        pauseMusic()
    }

    fun exitListenCall(view: View) {
        //停止服务
        stopService(this.intentService)
        finish()
    }

    /**
     * 启动来电监听的方法
     */
    @RequiresApi(Build.VERSION_CODES.O)
    fun stopStart1Call(view: View) {
        if(ContextCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE)
                !=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,
                    arrayOf(Manifest.permission.CALL_PHONE,Manifest.permission.READ_CALL_LOG,Manifest.permission.ANSWER_PHONE_CALLS),1)
        }else{
            startService(Intent(this,ListenCallService::class.java))
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        when(requestCode){
            1->{
                if (grantResults.isNotEmpty()&&grantResults[0]==PackageManager.PERMISSION_GRANTED&&
                        grantResults[1]==PackageManager.PERMISSION_GRANTED&&
                        grantResults[2]==PackageManager.PERMISSION_GRANTED){
                    startService(Intent(this,ListenCallService::class.java))
                }
                else{
                    Toast.makeText(this,"没有授予权限！",Toast.LENGTH_SHORT).show()
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    /**
     * 停止来电监听的方法
     */
    fun exitStop1Call(view: View) {
        stopService(Intent(this,ListenCallService::class.java))
    }


}
